﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Course.Entity;
using PowerCreator.LMS.Web.Catalog;
using PowerCreator.LMS.Web.Course;

public partial class sysadmin_CourseManager_CourseSelect :PowerCreator.LMS.Web.Base.BasePage
{
    CatalogCourse opCatalogCourse = new CatalogCourse();
    protected Course opCourse = new Course();
    Catalog opCatalog = new Catalog();

    #region SQL语句部分

    private string _SQL_CatalogCourse = "select FKCatalogcouCourseID from TCatalogCourse where FKCatalogcouCatalogID={0}";

    #endregion

    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "../login.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        CheckUser();
        if (!IsPostBack)
        {
            ViewState["CatalogID"] = 0;
            ViewState["SearchType"] = "0";  //0简单搜索1高级搜索
            ViewState["OrderBy"] = "";      //排序字段
            ViewState["OrderType"] = " desc ";//排序方式

            
            BinderCourseType();
            BinderCourseState();

          
            string catalogid = Request.QueryString["CatalogID"].ToString();
            ViewState["CatalogID"] = catalogid;
            InitPage();

            string search = GetSearch();
            BinderPager(search);
            BinderRPT(search, 1);
        }
    }
    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectNewUrl(url);
        }
        else
        {
            login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            if (login == null)
            {
                Script.RedirectNewUrl(url);
            }
            else
            {
                if (login.VOAdvancedUser == null)
                {
                    Script.RedirectNewUrl(url);
                }
                else if (login.VOAdvancedUser.AUserGroup > 2)
                {
                    //Response.Write(login.VOAdvancedUser.AUserGroup.ToString());
                    //Response.End();
                    Script.ShowAndHistory("您无此权限！", -1);
                }
            }
        }
    }

    protected override void InitPage()
    {
        lbl_Tit_InfoTitle.Text = GetLanageValueByKey("lbl_CourseSelect_Tit_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_CourseName.Text = GetLanageValueByKey("lbl_CourseSelect_CourseName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_TeacherName.Text = GetLanageValueByKey("lbl_CourseSelect_TeacherName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_CourseType.Text = GetLanageValueByKey("lbl_CourseSelect_CourseType", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_CourseState.Text = GetLanageValueByKey("lbl_CourseSelect_CourseState", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Search.Text = GetLanageValueByKey("btn_CourseSelect_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Add.Text = GetLanageValueByKey("btn_CourseSelect_Add", PowerCreator.LMS.Language.LanagetItemType.Page);

        btn_Back.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        chkAll.Text = GetLanageValueByKey("RoleUser_rdbtn_YesNoAll", PowerCreator.LMS.Language.LanagetItemType.Page);
        chkCancel.Text = GetLanageValueByKey("SetBaseAction_rdbtn_All", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_SortSearch.Text = GetLanageValueByKey("btn_CourseSelect_Search", PowerCreator.LMS.Language.LanagetItemType.Page);

       
        VOTCatalog vo = opCatalog.Load(int.Parse(ViewState["CatalogID"].ToString()));
        lbl_InfoTitle.Text = vo.CatalogName;
        string search1 = string.Format("FKCatalogcouCatalogID = {0}", ViewState["CatalogID"]);
        lbl_CourseResourceCount.Text = opCatalogCourse.QueryCatalogCourseCount(search1).ToString();

        base.InitPage();
    }

    protected void BinderCourseType()
    {
        ListItem li = new ListItem();
        li.Text = GetLanageValueByKey("Course_db_d0", PowerCreator.LMS.Language.LanagetItemType.Page);
        li.Value = "0";

        ListItem li0 = new ListItem();
        li0.Text = GetLanageValueByKey("Course_db_d1", PowerCreator.LMS.Language.LanagetItemType.Page);
        li0.Value = "1";
        ListItem li1 = new ListItem();
        li1.Text = GetLanageValueByKey("Course_db_d2", PowerCreator.LMS.Language.LanagetItemType.Page);
        li1.Value = "2";

        ListItem li2 = new ListItem();
        li2.Text = GetLanageValueByKey("Course_db_d3", PowerCreator.LMS.Language.LanagetItemType.Page);
        li2.Value = "3";

        ddl_CourseType.Items.Add(li);
        ddl_CourseType.Items.Add(li0);
        ddl_CourseType.Items.Add(li1);
        ddl_CourseType.Items.Add(li2);
    }
    protected void BinderCourseState()
    {
        ListItem li = new ListItem();
        li.Text = GetLanageValueByKey("AddCourseInfo_db_d2", PowerCreator.LMS.Language.LanagetItemType.Page);
        li.Value = "0";
        ListItem li0 = new ListItem();
        li0.Text = GetLanageValueByKey("AddCourseInfo_db_d1", PowerCreator.LMS.Language.LanagetItemType.Page);
        li0.Value = "1";
        ListItem li1 = new ListItem();
        li1.Text = GetLanageValueByKey("Course_db_d0", PowerCreator.LMS.Language.LanagetItemType.Page);
        li1.Value = "2";

        ddl_CourseState.Items.Add(li1);
        ddl_CourseState.Items.Add(li);
        ddl_CourseState.Items.Add(li0);
    }
    private string GetSearch()
    {
        StringBuilder sbSearch = new StringBuilder();

        sbSearch.Append("Course_Id not in (");
        sbSearch.Append(string.Format(_SQL_CatalogCourse, ViewState["CatalogID"]));
        sbSearch.Append(") and ");
        if (ViewState["SearchType"].ToString() == "1")
        {
            if (!string.IsNullOrEmpty(txt_CourseName.Text))
            {
                sbSearch.Append(string.Format(" CourseName like '%{0}%' and ", txt_CourseName.Text));
            }
            if (!string.IsNullOrEmpty(txt_CourseMostlyTeacherName.Text))
            {
                sbSearch.Append(string.Format(" CourseMostlyTeacherName like '%{0}%' and ", txt_CourseMostlyTeacherName.Text));
            }
            if (ddl_CourseType.SelectedValue.ToString() != "0")
            {
                sbSearch.Append(string.Format(" CourseIsCompulsory = {0} and ", ddl_CourseType.SelectedValue.ToString()));
            }
            if (ddl_CourseState.SelectedValue.ToString() != "2")
            {
                sbSearch.Append(string.Format(" CourseState = {0} and ", ddl_CourseState.SelectedValue.ToString()));
            }
        }
        else if (ViewState["SearchType"].ToString() == "0")
        { 
            //简单搜索
            sbSearch.Append(string.Format(" (CourseName like '%{0}%' or CourseMostlyTeacherName like '%{0}%' or Description like '%{0}%') ", txt_SearchKey.Text));
        }
        if (sbSearch.ToString().EndsWith(" and "))
            sbSearch.Remove(sbSearch.Length - 4, 4);

        return sbSearch.ToString();
    }
    private void BinderPager(string search)
    {
        AspNetPager1.PageSize = LMSPageSize;
        AspNetPager1.RecordCount = opCourse.QueryCourseCount(search);
    }
    private void BinderRPT(string search,int pageIndex)
    {
        string orderby = ViewState["OrderBy"].ToString();
        if (!string.IsNullOrEmpty(orderby))
        {
            orderby += ViewState["OrderType"].ToString();
        }

        DataTable dt = opCourse.QueryCourse(search,orderby, LMSPageSize,pageIndex);
        rptCourse.DataSource = dt;
        rptCourse.DataBind();
        PreAspNetPager(AspNetPager1);
    }
    protected void rptCourse_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "leditor")
        {
            PowerCreator.LMS.Web.CreatedByInfo cinfo = GetCreatedByInfo();
            string searcht = string.Format("FKCatalogcouCatalogID={0} and FKCatalogcouCourseID={1}", ViewState["CatalogID"], e.CommandArgument.ToString());
            if (opCatalogCourse.QueryCatalogCourseCount(searcht) == 0)
            {
                VOTCatalogCourse vo = new VOTCatalogCourse();
                vo.CatalogcouCreatetime = cinfo.CreatedTime;
                vo.CatalogcouCreatorname = cinfo.UserName;
                vo.CatalogcouIndex = 0;
                vo.CatalogcouResIsQueue = 0;
                vo.CatalogcouTerm = 0;
                vo.FKCatalogcouCatalogID = int.Parse(ViewState["CatalogID"].ToString());
                vo.FKCatalogcouCourseID = int.Parse(e.CommandArgument.ToString());
                vo.FKCatalogcouCreatorID = cinfo.UserID;
                if (vo.FKCatalogcouCatalogID != 0)
                {
                    if (opCatalogCourse.AddCatalogCourse(vo))
                    {
                        string search = GetSearch();
                        BinderPager(search);
                        BinderRPT(search, AspNetPager1.CurrentPageIndex);
                    }
                }
            }
            InitPage();
        }
        else if (e.CommandName == "orderby")
        {
            string search = GetSearch();
            BinderRPT(search, AspNetPager1.CurrentPageIndex);
            if (ViewState["OrderType"].ToString() == " desc")
                ViewState["OrderType"] = " asc";
            else
                ViewState["OrderType"] = " desc";
        }
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        ViewState["SearchType"] = "1";
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, 1);
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        PowerCreator.LMS.Web.CreatedByInfo cinfo = GetCreatedByInfo();
        
        for (int i = 0; i < rptCourse.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptCourse.Items[i].FindControl("chkSelect");
            if (cbox.Checked)
            {
                Label lblid = (Label)rptCourse.Items[i].FindControl("lblid");
                string tsearch = string.Format("FKCatalogcouCatalogID={0} and FKCatalogcouCourseID={1}", ViewState["CatalogID"], lblid.Text);
                if (opCatalogCourse.QueryCatalogCourseCount(tsearch) == 0)
                {
                    VOTCatalogCourse vo = new VOTCatalogCourse();
                    vo.CatalogcouCreatetime = cinfo.CreatedTime;
                    vo.CatalogcouCreatorname = cinfo.UserName;
                    vo.CatalogcouIndex = 0;
                    vo.CatalogcouResIsQueue = 0;
                    vo.CatalogcouTerm = 0;
                    vo.FKCatalogcouCatalogID = int.Parse(ViewState["CatalogID"].ToString());
                    vo.FKCatalogcouCourseID = int.Parse(lblid.Text);
                    vo.FKCatalogcouCreatorID = cinfo.UserID;
                    if (vo.FKCatalogcouCatalogID != 0)
                        opCatalogCourse.AddCatalogCourse(vo);
                }
            }
        }

        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, AspNetPager1.CurrentPageIndex);

        InitPage();
    }
    protected void btn_Back_Click(object sender, EventArgs e)
    {
        string url = "Course.aspx";
        Script.RedirectURL(url);
    }
    protected void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        chkCancel.Checked = false;
        //全选/取消
        for (int i = 0; i < rptCourse.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptCourse.Items[i].FindControl("chkSelect");
            if (chkAll.Checked)
                cbox.Checked = true;
            else
                cbox.Checked = false;
        }
    }
    protected void chkCancel_CheckedChanged(object sender, EventArgs e)
    {
        chkAll.Checked = false;
        //反选
        for (int i = 0; i < rptCourse.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptCourse.Items[i].FindControl("chkSelect");
            if (cbox.Checked)
                cbox.Checked = false;
            else
                cbox.Checked = true;
        }
    }
    protected void btn_SortSearch_Click(object sender, EventArgs e)
    {
        ViewState["SearchType"] = "0";
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search,AspNetPager1.CurrentPageIndex);

       
    }
}
